clear  
 cd "${main_loc}"

 
 
use "${main_loc}/data_baseline_WTR",replace
************************************************************************************************************************
**# LOOP
************************************************************************************************************************
 

drop if partner=="Ukraine"	
drop if partner=="Turkey"	
drop if partner=="Kazakhstan"	
drop if partner=="Belarus"	
drop if partner=="Serbia"	
drop if partner=="Armenia"
drop if partner=="Uzbekistan"
drop if partner=="Kyrgyzstan"
drop if partner=="Georgia"
drop if partner=="Azerbaijan"


levelsof iso2_o, local(levels) 

foreach c of local levels{
preserve

keep if iso2_o=="`c'"
 

	di "`c'"
 merge n:1 reporter partner using "${main_loc}/export_similarities_Russia_WTR.dta"
capture drop s1_* s2_* s3_* s4_* s5_*  
drop if _m!=3
replace s1=0 if s1==.
replace s2=0 if s2==.
replace s3=0 if s3==.
replace s4=0 if s4==.
replace s5=0 if s5==.

egen id=group(iso2_d),label
egen t=group(date)
tsset id t

drop if BVlndist==.
drop if BVcontiguity==.
drop if BVcommon_language==.
drop if lnagdp_wdi_const==.
 
tsfill,full
replace value=0 if value==.

sort id t
local varl  BVlndist BVcontiguity BVcommon_language lnagdp_wdi_const  max_agree_pta_goods max_agree_cu 
foreach x of local varl {  
sort id `x' 
by id: carryforward `x' ,replace
}

 
sort id t
gen date_new=date  
order id date date_new 

bys id: replace date_new=l.date_new+1 if date_new==.
bys id: replace date_new=l2.date_new+2 if date_new==.
bys id: replace date_new=l3.date_new+3 if date_new==.
bys id: replace date_new=l4.date_new+4 if date_new==.
bys id: replace date_new=l5.date_new+5 if date_new==.
bys id: replace date_new=f.date_new-1 if date_new==.
bys id: replace date_new=f2.date_new-2 if date_new==.
bys id: replace date_new=f3.date_new-3 if date_new==.
bys id: replace date_new=f4.date_new-4 if date_new==.
bys id: replace date_new=f5.date_new-5 if date_new==.
format date_new %tm
drop date fdate year  month
rename date_new date

gen fdate = dofm(date)
gen year=year(fdate)
gen month=month(fdate)
gen dm=month



 
 keep if t>=13

sort id t

summ id if partner=="Russia"
capture   synth   value   value BVlndist BVcontiguity BVcommon_language  lnagdp_wdi_const  s1 s2  s3  s4  max_agree_pta_goods ,   trunit("`r(mean)'") trperiod(30)    keep("`c'_Russia_placebo",replace) 
 summ id if partner=="Russia"
capture synth_asmd, trunit("`r(mean)'") trperiod(30) depvar(value) data("`c'_Russia_placebo.dta")
sca `c'_Russia_mean=r(mean)
sca test=r(mean)
gen soglia=0.4
sca `c'_Russia_max=r(max)
sum soglia
if test>r(mean) {
capture erase "`c'_Russia_placebo.dta"  
 }
 
 restore
}




 foreach c of local levels {   
    clear 
capture use "${main_loc}/`c'_Russia_placebo.dta"
capture keep _Y_treated _Y_synthetic _time
capture gen iso2_o="`c'"
capture drop if iso2_o==""
capture drop if _time==.

capture save  "${main_loc}/`c'_pre_append_Russia_placebo.dta",replace
}
  
 
 
 
 
  
 
  
clear
cd "${main_loc}"
local theFiles: dir . files "*_pre_append_Russia_placebo.dta" 

append using `theFiles' 
 
**********
gen keep=1 if _time==48
bys iso2_o: egen insample=max(keep)
drop if insample==.
drop if _time>50
egen id=group(iso2_o)
tsset id _time
 
drop if iso2_o=="UA"
 
 drop if _time>37
***FIGURE 1 and 2
preserve
rename iso2_o iso2
merge n:1 iso2 using "${main_loc}/sanctioning_list_WTR.dta" 
drop if _m == 2
drop _m
replace sanctioning=0 if sanctioning==.
collapse (sum) _Y_treated _Y_synthetic,by(_time sanctioning)
reshape wide _Y_treated _Y_synthetic, i(_time) j(sanctioning)
gen diffp0=((_Y_treated0-_Y_synthetic0)/_Y_synthetic0)*100
gen diffp1=((_Y_treated1-_Y_synthetic1)/_Y_synthetic1)*100
*Figure 2
tsline diffp0 diffp1
graph export "${main_loc}/Figure_8b.png",replace
*Figure 1
gen _Y_treated_tot=(_Y_treated0+_Y_treated1)/1000000000
gen _Y_synthetic_tot=(_Y_synthetic0+_Y_synthetic1)/1000000000
tsline _Y_treated_tot _Y_synthetic_tot
graph export "${main_loc}/Figure_8a.png",replace
save "${main_loc}/Figure_8.dta",replace
restore
 
